package com.lions.client.view.basepanel;

import java.util.LinkedList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.lions.client.model.Model;
import com.lions.client.model.Module;
import com.lions.client.model.ModuleList;
import com.lions.client.view.mappanel.MapPanel;

public class MinBaseBuilder {
	public MinBaseBuilder(Model	m, MapPanel map, 
			int plain1X, int plain1Y, 
			int plain2X, int plain2Y, 
			int plain3X, int plain3Y, 
			int airlockX, int airlockY, 
			int controlX, int controlY, 
			int powerX, int powerY, 
			int foodX, int foodY, 
			int dormX, int dormY, 
			int canteenX, int canteenY, 
			int sanX, int sanY){
		
		rows = 50;
		columns = 100;
		model =	m;
		this.map = map.getMap(); 
		this.plain1X = plain1X; 
		this.plain1Y = plain1Y; 
		this.plain2X = plain2X;
		this.plain2Y = plain2Y; 
		this.plain3X = plain3X;
		this.plain3Y = plain3Y; 
		this.airlockX = airlockX;
		this.airlockY = airlockY; 
		this.controlX = controlX;
		this.controlY = controlY; 
		this.powerX = powerX;
		this.powerY = powerY; 
		this.foodX = foodX;
		this.foodY = foodY; 
		this.dormX = dormX;
		this.dormY = dormY; 
		this.canteenX = canteenX;
		this.canteenY = canteenY; 
		this.sanX = sanX;
		this.sanY = sanY;
	}
		public void build(){
		//clear the map for building
				map.clear();
				
				//define a 2d container to populate with base modules
				Module[][] container = new Module[rows][columns];
				
				
				//get the moduleList
				ModuleList moduleList = model.getModuleList();
				
				//put the modules in the container
				for(Module module : moduleList){
					if((module.getX() < columns) && (module.getY() < rows))
					container[module.getY()][module.getX()] = module;
				}
				
				//A list to hold the plain Modules
				LinkedList<Module> plainList = new LinkedList<Module>();
				
				//define variables for finding the center of the base
				int centerX = this.findCenterX(container);
				int centerY = this.findCenterY(container);
				
				//find the center module: plain module
				for(Module module : moduleList){
					if(module.getType().toString().equals("PLAIN")){
						plainList.add(module);
					}
				}
				//these two checks can probably be removed safely
				for(Module module : plainList){
					if(module.getType().toString() != "PLAIN"){
						System.out.println("Something went wrong making the connector list");
					}
				}
				if(plainList.size() < 3){
					Window.alert("Not enough Connectors");
				}
				
				//put the connectors into the 2d base container
				if(!plainList.isEmpty()){
				container[centerY + plain1Y][centerX + plain1X] = plainList.getFirst();
				plainList.remove();
				}
				if(!plainList.isEmpty()){
				container[centerY + plain2Y][centerX + plain2X] = plainList.getFirst();
				plainList.remove();
				}
				if(!plainList.isEmpty()){
				container[centerY + plain3Y][centerX + plain3X] = plainList.getFirst();
				plainList.remove();
				}
				
				//find a canteen
				for(Module module : moduleList){
					if(module.getType().toString() == "CANTEEN"){
						container[centerY + canteenY][centerX + canteenX] = module;
					}
				}
				
				//find an airlock
				for(Module module : moduleList){
					if(module.getType().toString() == "AIRLOCK"){
						container[centerY + airlockY][centerX + airlockX] = module;
					}
				}
				
				//find food and water storage
				for(Module module : moduleList){
					if(module.getType().toString() == "FOOD_WATER_STORAGE"){
						container[centerY + foodY][centerX + foodX] = module;
					}
				}
				
				//find control
				for(Module module : moduleList){
					if(module.getType().toString() == "CONTROL"){
						container[centerY + controlY][centerX + controlX] = module;
					}
				}
				
				//find power
				for(Module module : moduleList){
					if(module.getType().toString() == "POWER"){
						container[centerY + powerY][centerX + powerX] = module;
					}
				}
				
				//find sanitation
				for(Module module : moduleList){
					if(module.getType().toString() == "SANITATION"){
						container[centerY + sanY][centerX + sanX] = module;
					}
				}
				
				//find dorms
				for(Module module : moduleList){
					if(module.getType().toString() == "DORMITORY"){
						container[centerY + dormY][centerX + dormX] = module;
					}
				}
				
				//set the new map
				int inverter = 0;
				for (int row = rows -1; row >= 0; row--) {
					for (int col = columns - 1; col >= 0; col--) {
						if (container[row][col] != null) {
							map.setWidget(inverter, col, new Image(
									"images/" + container[row][col].getType() + ".jpg"));
						}
					}
				inverter++;
				}
				
				//alert movement points
				Window.alert("Movement Points for this Base = " + calculateMovementPoints(container));
	
			}
	
	
		public int calculateMovementPoints(Module[][] container){
			int movementPoints = 0;
			for (int row = 9; row >= 0; row--) {
				for (int col = 29; col >= 0; col--) {
					if (container[row][col] != null) {
						movementPoints = movementPoints + Math.abs(row - container[row][col].getY());
						movementPoints = movementPoints + Math.abs(col - container[row][col].getX());
					}
				}}
			return movementPoints;		
		}
		
		/**
		 * Finds the center X value for the current Modules
		 * @param container The 2d container holding the modules
		 * @return An integer value representing the mean value of X
		 */
		public int findCenterX(Module[][] container){
			int tempX = 0;
			int numModules = 0;
			for (int row = 49; row >= 0; row--) {
				for (int col = 99; col >= 0; col--) {
					if (container[row][col] != null) {
						numModules++;
						tempX = tempX + container[row][col].getX();
					}
				}
			}
			tempX = tempX/numModules;
			if(tempX == 0){
				tempX = 1;
			}
			if(tempX == columns - 1){
				tempX = columns - 3;
			}
			
			return tempX;
		}
		
		/**
		 * Finds the center Y value for the current Modules
		 * @param container The 2d container holding the modules
		 * @return An integer value representing the mean value of Y
		 */
		public int findCenterY(Module[][] container){
			int tempY = 0;
			int numModules = 0;
			for (int row = 49; row >= 0; row--) {
				for (int col = 99; col >= 0; col--) {
					if (container[row][col] != null) {
						numModules++;
						tempY = tempY + container[row][col].getY();
					}
				}
			}
			tempY = tempY/numModules;
			if(tempY == 0){
				tempY = 1;
			}
			if(tempY == rows - 1){
				tempY = rows - 3;
			}
			return tempY;
		}
		
		int rows;
		int columns;
		Model	model; 
		Grid map; 
		int plain1X, plain1Y, plain2X, plain2Y, plain3X, plain3Y, airlockX, airlockY; 
		int controlX, controlY, powerX, powerY, foodX, foodY, dormX, dormY; 
		int canteenX, canteenY, sanX, sanY;
	}

